/**
 * 
 */
package cn.bblocks.cacheTest.suport;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;

/**
 * <p>Description:fastjson工具类 <／p>
 * <p>Company: sx<／p> 
 * @author jzhao
 *
 * @date 2019年5月17日
 */
public class FastJsonUtils {
	 private static final SerializeConfig config;
	    static {
	        config = new SerializeConfig();
//	        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
//	        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
//	        config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
	        
	    }
	 
	    public static final SerializerFeature[] features = {
	    		//格式化的
	    		//SerializerFeature.PrettyFormat,
                //SerializerFeature.WriteNullNumberAsZero,
                //SerializerFeature.WriteNullListAsEmpty,
                //SerializerFeature.WriteMapNullValue,
                //SerializerFeature.DisableCheckSpecialChar
                SerializerFeature.WriteNullStringAsEmpty,
                //重复对象引用  Fastjson-fastjson中$ref对象重复引用问题
                SerializerFeature.DisableCircularReferenceDetect,
                //时间格式化“yyyy-MM-dd HH:mm:ss”
                SerializerFeature.WriteDateUseDateFormat
              
	    };
	 
	    public static String convertObjectToJSON(Object object) {
	        return JSON.toJSONString(object, config, features);
	    }
	    
	    public static String toJSONNoFeatures(Object object) {
	        return JSON.toJSONString(object, config);
	    }
	    
	 
	 
	    public static Object toBean(String text) {
	        return JSON.parse(text);
	    }
	 
	    public static <T> T toBean(String text, Class<T> clazz) {
	        return JSON.parseObject(text, clazz);
	    }
	 
	    /**
	     *  转换为数组
	     * @param text
	     * @return
	     */
	    public static <T> Object[] toArray(String text) {
	        return toArray(text, null);
	    }
	 
	    /**
	     *  转换为数组
	     * @param text
	     * @param clazz
	     * @return
	     */
	    public static <T> Object[] toArray(String text, Class<T> clazz) {
	        return JSON.parseArray(text, clazz).toArray();
	    }
	 
	    /**
	     * 转换为List
	     * @param text
	     * @param clazz
	     * @return
	     */
	    public static <T> List<T> toList(String text, Class<T> clazz) {
	        return JSON.parseArray(text, clazz);
	    }
	 
	    /**
	     * 将string转化为序列化的json字符串
	     * @param keyvalue
	     * @return
	     */
	    public static Object textToJson(String text) {
	        Object objectJson  = JSON.parse(text);
	        return objectJson;
	    }
	    
	    /**
	     * json字符串转化为map
	     * @param s
	     * @return
	     */
	    public static <K, V> Map<K, V>  stringToCollect(String s) {
	        Map<K, V> m = (Map<K, V>) JSONObject.parseObject(s);
	        return m;
	    }
	    
	    /**
	     * 转换JSON字符串为对象
	     * @param jsonData
	     * @param clazz
	     * @return
	     */
	    public static Object convertJsonToObject(String jsonData, Class<?> clazz) {
	        return JSONObject.parseObject(jsonData, clazz);
	    }
	    
	    /**
	     * 将map转化为string
	     * @param m
	     * @return
	     */
	    public static <K, V> String collectToString(Map<K, V> m) {
	        String s = JSONObject.toJSONString(m);
	        return s;
	    }
	    
	    public static void main(String[] args) {
	    	Map m = new HashMap();
	    	m.put(1, new Date());
	    	System.out.println(convertObjectToJSON(m));
	    	
	    	
	    	String dateJson = JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat);
	    	        
	    	System.out.println(dateJson);
	    }
}
